if not pattern then pattern = {} end

if not pattern.gap then

dofile (ScriptsRootPath .. "lib\\lib.lua")

pattern.gap = {}

pattern.gap.patterns_init = lib.path.init .. "patterns.ini"

function pattern.gap.OnCandle(source, index, data)
	if not source then return end
	if not pattern.gap.params then
		pattern.gap.params = lib:LoadIni(pattern.gap.patterns_init, "gap", {gap = 0.1})
		if processor and processor.log then
			processor:log("pattern.gap.params", pattern.gap.params)
		end
	end
	
	if not index then index = 1 end
	
	local candle = {
		lib:GetLastCandle(source, index + 1),
		lib:GetLastCandle(source, index)
	}
	--processor:log("gap", source, candle[1])--, candle[1].instrument)
	if not (candle[1] and candle[1].instrument and candle[2]) then return end
	
	if not data then data = {} end
	
	if not data.low or data.low > candle[2].low then
		data.low = candle[2].low
	end
	if not data.high or data.high < candle[2].high then
		data.high = candle[2].high
	end
	
	local gap, price, is_long
	
	if candle[1].close < data.low then
		is_long = true
		gap = data.low - candle[1].close
		price = candle[1].close + candle[1].instrument.min_price_step
	elseif candle[1].close > data.high then
		is_long = false
		gap = candle[1].close - data.high
		price = candle[1].close - candle[1].instrument.min_price_step
	end
	if gap and price then
		if not Settings and gap < pattern.gap.params.gap then return end
				
		--candle[1].datetime.labels__ = {
		--	gap = price
		--}
		candle[1].datetime.gap__ = gap
		candle[1].datetime.is_long__ = is_long
		candle[1].datetime.hint__ = string.format("%s\n%.2f%%", lib:FormatPrice(price, candle[1].instrument), gap * 100 / price)
		candle[1].datetime.notify__ = string.format("%s %s %.2f%%", candle[1].instrument.code, candle[1].instrument.short_name, gap * 100 / price)
		--candle[1].datetime.notify__ = candle[1].instrument.code .. " " .. candle[1].instrument.short_name .. " " .. candle[1].datetime.hint__
		--processor:log(candle[1].datetime.notify__)
		return price, candle[1].datetime
	end
end

end -- of file
